package com.souku.finance.application.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2016/6/6.
 */
public class JsonUtil {


    /**
     * 解析得到Code
     *
     * @param response
     * @return
     */
    public static int getCode(String response) {
        JSONObject object = null;
        try {
            object = new JSONObject(response);
            return object.getInt("code");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * response字符串转JSONObject
     *
     * @param response
     * @return
     */
    public static JSONObject getJSONObject(String response) {
        JSONObject object = null;
        try {
            object = new JSONObject(response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
    }


    /**
     * data字符串转JSONObject
     *
     * @param response
     * @return
     */
    public static JSONObject getData(String response) {
        try {
            return getJSONObject(response).getJSONObject("data");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JSONObject getUserInfo(String response) {
        try {
            return getJSONObject(response).getJSONObject("userInfo");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 通过键获取对应值
     *
     * @param object JSONObject对象
     * @param key    键
     * @return
     */
    public static String getString(JSONObject object, String key) {

        try {
            return object.getString(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 通过键获取对应值
     *
     * @param object JSONObject对象
     * @param key    键
     * @return
     */
    public static boolean getBoolean(JSONObject object, String key) {

        try {
            return object.getBoolean(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 提示错误信息
     *
     * @param response
     * @return
     */
    public static String getMessage(String response) {
        JSONObject object = null;
        try {
            object = new JSONObject(response);
            return object.getString("message");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getErrorMessage(String response) {
        JSONObject object = null;
        try {
            object = new JSONObject(response);
            return object.getString("errorMessage");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static int getErrorCode(String response) {
        JSONObject object = null;
        try {
            object = new JSONObject(response);
            return object.getInt("errorCode");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 解析实体
     *
     * @param response
     * @return
     */
    public static <T> T fastBean(String response, Class<T> classOfT) {
        Gson gson = new Gson();
        return gson.fromJson(response, classOfT);
    }

//    public <T> List<T> fastArrayBean(String response, Class<T> classOfT) {
//        try {
//            JSONObject jsonObject = new JSONObject(response);
//            String data = jsonObject.getString("data");
//            if (!TextUtils.isEmpty(data)) {
//                Type genericSuperclass = classOfT.getClass().getGenericSuperclass();
//                if (genericSuperclass instanceof ParameterizedType) {
//                    Type type = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
//                    Type arrayType = $Gson$Types.newParameterizedTypeWithOwner(null
//                            , ArrayList.class, type);
//                    return new Gson().fromJson(data, arrayType);
//                }
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        return new ArrayList<T>();
//    }
}
